package com.sdp.right.control;


import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.threadLocal.ContextManager;
import com.sdp.core.bizc.utils.CoreConstantsEnum;
import com.sdp.core.bizc.utils.SnowFlake;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.db.sys_flow_current_task.service.ISysFlowCurrentTaskService;
import com.sdp.right.db.sys_flow_instance.service.ISysFlowInstanceService;
import com.sdp.right.db.sys_flow_node_attribute_version.service.ISysFlowNodeAttributeVersionService;
import com.sdp.right.db.sys_flow_node_jump_version.service.ISysFlowNodeJumpVersionService;
import com.sdp.right.db.sys_flow_task_his.service.ISysFlowTaskHisService;
import com.sdp.right.db.sys_flow_template.service.ISysFlowTemplateService;
import com.sdp.right.db.sys_flow_template_version.service.ISysFlowTemplateVersionService;
import com.sdp.right.entity.SysFlowTemplate;
import com.sdp.right.entity.SysFlowTemplateVersion;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-07-09
 */
@RestController
@RequestMapping("/SysFlowTemplateController")
public class SysFlowTemplateController {

	@Autowired
	private ISysFlowTemplateService iSysFlowTemplateService;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@Autowired
	private SnowFlake snowFlake;
	
	@Autowired
	private Query query;

	@Autowired
	private ISysFlowTemplateVersionService iSysFlowTemplateVersionService;
	
	@Autowired
	private ISysFlowNodeAttributeVersionService iSysFlowNodeAttributeVersionService;
	
	@Autowired
	private ISysFlowNodeJumpVersionService iSysFlowNodeJumpVersionService;
	
	@Autowired
	private ISysFlowInstanceService iSysFlowInstanceService;
	
	@Autowired
	private ISysFlowCurrentTaskService iSysFlowCurrentTaskService;
	
	@Autowired
	private ISysFlowTaskHisService iSysFlowTaskHisService;
	
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iSysFlowTemplateService, SysFlowTemplate.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody SysFlowTemplate sysFlowTemplate) {
		if(sysFlowTemplate==null) {
			return Result.error("流程模版信息为空");
		}
		if(StringUtils.isEmpty(sysFlowTemplate.getKeyId())) {
			sysFlowTemplate.setCreateTime(LocalDateTime.now());
			sysFlowTemplate.setTemplateNo(snowFlake.nextId()+"");
			sysFlowTemplate.setSystemNo(ContextManager.getContextData(CoreConstantsEnum.header中的模块参数名.getCode()));
			Result<?> r = uniqueCheckTools.isUniqueSingle(iSysFlowTemplateService, SysFlowTemplate.class, new String[] {"templateName"}, "keyId",sysFlowTemplate, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("此模板名称已经存在");
			}
			iSysFlowTemplateService.save(sysFlowTemplate);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iSysFlowTemplateService, SysFlowTemplate.class, new String[] {"templateName"}, "keyId",null, sysFlowTemplate, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("此模板名称已经存在");
			}
			iSysFlowTemplateService.updateById(sysFlowTemplate);
		}
		return Result.ok();
	}
	
	/**
	 * 删除
	 * @param keyId
	 * @return
	 */
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId){
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("流程信息不完整，不能删除");
		}
		SysFlowTemplate sysFlowTemplate = iSysFlowTemplateService.getById(keyId);
		if(sysFlowTemplate==null) {
			return Result.error("此流程数据不存在，不能删除");
		}
		iSysFlowTemplateService.removeFlowTemplateByTemplateNo(sysFlowTemplate.getTemplateNo());
		iSysFlowTemplateVersionService.removeFlowTemplateVersionByTemplateNo(sysFlowTemplate.getTemplateNo());
		iSysFlowNodeAttributeVersionService.removeFlowNodeAttributeVersion(sysFlowTemplate.getTemplateNo());
		iSysFlowNodeJumpVersionService.removeFlowNodeJumpVersion(sysFlowTemplate.getTemplateNo());
		return Result.ok();
	}
	
	
	/**
	 * 清理历史
	 * @param keyId
	 * @return
	 */
	@GetMapping(value = "/clearHis")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> clearHis(@RequestParam(name="keyId",required=true) String keyId){
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("流程信息不完整，不能清理");
		}
		SysFlowTemplate sysFlowTemplate = iSysFlowTemplateService.getById(keyId);
		if(sysFlowTemplate==null) {
			return Result.error("此流程数据不存在，不能清理");
		}
		iSysFlowTemplateVersionService.clearHisFlowTemplateVersion(sysFlowTemplate.getTemplateVersion(),sysFlowTemplate.getTemplateNo());
		iSysFlowNodeAttributeVersionService.clearHisFlowNodeAttributeVersion(sysFlowTemplate.getTemplateVersion(),sysFlowTemplate.getTemplateNo());
		iSysFlowNodeJumpVersionService.clearHisFlowNodeJumpVersion(sysFlowTemplate.getTemplateVersion(),sysFlowTemplate.getTemplateNo());
		iSysFlowInstanceService.clearHisFlowInstanceVersion(sysFlowTemplate.getTemplateVersion(),sysFlowTemplate.getTemplateNo());
		iSysFlowCurrentTaskService.clearHisFlowCurrentTaskVersion(sysFlowTemplate.getTemplateVersion(),sysFlowTemplate.getTemplateNo());
		iSysFlowTaskHisService.clearFlowTaskHisVersion(sysFlowTemplate.getTemplateVersion(),sysFlowTemplate.getTemplateNo());
		return Result.ok();
	}
	
	
	/**
	 * 部署
	 * @param keyId
	 * @return
	 */
	@GetMapping(value = "/deploy")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> publish(@RequestParam(name="keyId",required=true) String keyId){
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("流程信息不完整，不能部署");
		}
		SysFlowTemplate sysFlowTemplate = iSysFlowTemplateService.getById(keyId);
		if(sysFlowTemplate==null) {
			return Result.error("此流程数据不存在，不能部署");
		}
		//生成新的部署版本号
		String newVersion = snowFlake.nextId()+"";
		sysFlowTemplate.setTemplateVersion(newVersion);
		iSysFlowTemplateService.updateById(sysFlowTemplate);
		
		//生成版本相关信息
		SysFlowTemplateVersion sysFlowTemplateVersion = iSysFlowTemplateVersionService.createFlowTemplateVersion(sysFlowTemplate);
		
		//生成版本对应的节点属性信息
		iSysFlowNodeAttributeVersionService.createFlowNodeAttributeVersion(sysFlowTemplateVersion);
		
		//生成版本对应的节点跳转信息
		iSysFlowNodeJumpVersionService.createFlowNodeJumpVersion(sysFlowTemplateVersion);
		return Result.ok();
	}
}

